package com.atguigu.gmall.product.controller;


import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 *
 */
@RequestMapping("/admin/product")
@RestController
public class BaseAttrInfoController {

    @Autowired
    BaseAttrInfoService baseAttrInfoService;

    @Autowired
    BaseAttrValueService baseAttrValueService;


    /**
     * 根据分类id获取平台属性
     *
     * @return
     */
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result getAttrInfoList(@PathVariable("category1Id") Long category1Id,
                                  @PathVariable("category2Id") Long category2Id,
                                  @PathVariable("category3Id") Long category3Id) {


        //查出分类所有的属性名和值
        List<BaseAttrInfo> infos = baseAttrInfoService.getAttrInfoAndValueList(category1Id, category2Id, category3Id);

        return Result.ok(infos);
    }


    ///admin/product/saveAttrInfo

    /**
     * 保存平台属性
     * 前端提交的是JSON，转成 BaseAttrInfo
     *
     * @return
     * @RequestBody: 1、取出Post请求的请求体中的所有数据
     * 2、如果是json，SpringMVC帮我们转成 指定的类型BaseAttrInfo
     *
     * 是一个保存修改二合一的方法
     */
    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo info) {


        baseAttrInfoService.saveAttrInfo(info);

        return Result.ok();
    }


    /**
     * 根据平台属性id获取到属性的所有值
     *
     * @param attrId
     * @return
     */
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId) {

        QueryWrapper<BaseAttrValue> queryWrapper = new
                QueryWrapper<>();

        queryWrapper.eq("attr_id", attrId);

        List<BaseAttrValue> list = baseAttrValueService.list(queryWrapper);

        return Result.ok(list);
    }
}
